﻿$(function () {
    var page = 0;
    var _inCallback = false;
    function loadProducts() {
        if (page > -1 && !_inCallback) {
            _inCallback = true;
            page++;
            var catid = $('#input-catid').val();
            var url = "/Product/Index?Page=" + page;
            if (catid != undefined) {
                url = "/Product/Index?catId=" + catid + "&Page=" + page;
            }
            $('div#loading-product').html('<p><img src="/Images/loading.gif" alt="Loading..."></p>');
            setTimeout(function () {
                $.ajax({
                    type: "get",
                    url: url,
                    complete: function () {
                        $('div#loading-product').empty();
                    },
                    success: function (data) {
                        if ($.trim(data) != '') {
                            $("#product .container-product").append(data).fadeIn("slow");
                            $(".page-"+page+" img.lazy-load").lazyload({ threshold: "2000", effect: "fadeIn", effectspeed: 2000 });
                        }
                        else {
                            page = -1;

                        }
                        _inCallback = false;
                    }
                });
            },1000);
     
        }
    }
    $('#categories a').live('click', function () {
        page = 0;
    });
    $(window).on('scroll',function () {
        if ($(window).scrollTop() >= ($('#product').offset().top + $('#product').height() - 100) - $(window).height()) {
            loadProducts();
        }
    });
});